package com.serwer.gui;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import java.awt.Font;
import java.awt.Color;
import javax.swing.border.TitledBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class Logowanie extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField userLogowanie;
	private JPasswordField passLogowanie;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Logowanie frame = new Logowanie();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Logowanie() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 245, 295);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblMserwer = new JLabel("mSerwer");
		lblMserwer.setForeground(new Color(0, 128, 128));
		lblMserwer.setFont(new Font("Tahoma", Font.BOLD, 26));
		lblMserwer.setBounds(89, 200, 122, 23);
		contentPane.add(lblMserwer);
		
		JPanel panel = new JPanel();
		panel.setBorder(new TitledBorder(null, "Logowanie", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.setBounds(28, 11, 172, 151);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("U\u017Cytkownik:");
		lblNewLabel.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel.setBounds(10, 24, 67, 14);
		panel.add(lblNewLabel);
		
		JLabel lblNewLabel_1 = new JLabel("Has\u0142o:");
		lblNewLabel_1.setFont(new Font("Tahoma", Font.PLAIN, 11));
		lblNewLabel_1.setBounds(10, 69, 39, 14);
		panel.add(lblNewLabel_1);
		
		userLogowanie = new JTextField();
		userLogowanie.setBounds(76, 21, 86, 20);
		panel.add(userLogowanie);
		userLogowanie.setColumns(10);
		
		/*Logowanie do serwisu*/
		JButton btnNewButton = new JButton("Logowanie");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				String sql="Select * from user where status='1'";
			    try {
			    Class.forName("com.mysql.jdbc.Driver");
			    Connection con= (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/BROKER","root","");
			    Statement stmt=con.createStatement();
			    ResultSet rs = stmt.executeQuery(sql);
			    
			    String user=userLogowanie.getText();
			    String pwd= new String (passLogowanie.getPassword());
			    String md5 = null;
			    try {		             
			        MessageDigest digest = MessageDigest.getInstance("MD5");
			        digest.update(pwd.getBytes(), 0, pwd.length());
			        md5 = new BigInteger(1, digest.digest()).toString(16);
			        } catch (NoSuchAlgorithmException ee) {
			 
			            ee.printStackTrace();
			        }
			    while(rs.next()) {
			    String uname=rs.getString("username");
			    String password=rs.getString("password");
			    Boolean aktiv = rs.getBoolean("active");
			    
			    if ((user.equals(uname)) && (md5.equals(password)) && (aktiv == true))			 
			    {
			    	mainPanel mainP = new mainPanel(uname);
			    	mainP.setVisible(true);	
			}
			    }
			    }
			    catch (Exception ee){
			    JOptionPane.showMessageDialog(null, ee.getMessage());			    
			    }			
			}
		});
		btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 11));
		btnNewButton.setBounds(73, 110, 89, 23);
		panel.add(btnNewButton);
		
		passLogowanie = new JPasswordField();
		passLogowanie.setBounds(76, 66, 86, 20);
		panel.add(passLogowanie);
		
		JLabel label_4 = new JLabel("");
		label_4.setBounds(10, 94, 41, 48);
		panel.add(label_4);
		label_4.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\login.png"));
		
		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setLabelFor(this);
		lblNewLabel_2.setIcon(new ImageIcon("C:\\Users\\marmicc\\Desktop\\PRO\\Broker\\ico\\comp.png"));
		lblNewLabel_2.setBounds(10, 173, 69, 73);
		contentPane.add(lblNewLabel_2);
		
		
	}
	 public String quotate(String content){
		    
	        return "'"+content+"'";
	    }
}
